
const promise1 = new Promise((resolve, reject) => {
  setTimeout(resolve, 500, "one");
});

const promise2 = new Promise((resolve, reject) => {
  setTimeout(resolve, 100, "two");
  // setTimeout(reject, 100, "你是个好人");
});

const promises = [promise1, promise2];

// 一旦参数 promises 中的某个 promise 兑现 或 拒绝 ，
// 则 Promise.race 所返回的 promise 就会立即兑现或拒绝
const promise3 = Promise.race(promises);

const onFulfilled = value => {
    console.log(value);
}

const onRejected = reason => {
    console.log(reason);
}

// promise3 的状态 由 promises 数组中 最快的 promise 决定
// 若 promise3 状态转换为 fulfilled 则调用 onFulfilled 函数
// 若 promise3 状态转换为 rejected 则调用 onRejected 函数
promise3.then( onFulfilled , onRejected );

